<?php

use app\model\User;
use think\facade\Db;
use think\migration\Seeder;

class InitUser extends Seeder
{
    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    public function run() {
        // 初始化超级管理员
        $admin = User::where('username', '=', 'admin')->findOrEmpty();
        $admin->id = 1;
        $admin->username = 'admin';
        $admin->password = $admin->password ?? password_hash('123123', PASSWORD_DEFAULT);
        $admin->nickname = $admin->nickname ?? '超级管理员';
        $admin->save();

        $userList = [
            ['id' => 2, 'username' =>'user1', 'password' => password_hash('123123', PASSWORD_DEFAULT)],
            ['id' => 3, 'username' =>'user2', 'password' => password_hash('123123', PASSWORD_DEFAULT)],
            ['id' => 4, 'username' =>'user3', 'password' => password_hash('123123', PASSWORD_DEFAULT)],
        ];
        User::updateOrCreateByPkBatch($userList);
        // updateOrCreateByPkBatch(new User(), $userList);
    }
}